﻿using Common.Contract;
using Common.Contract.Customer.Identifier;
using Common.Contract.Customer.Record;
using Common.Contract.DataCenter.Record;
using Common.Contract.DataCenter.Request;
using Common.Contract.SaleOut.Record;
using Common.Contract.Warranty.Record;
using Common.Service.Command;
using Common.Service.Queue;
using Common.Util;
using DataCenter.Configuration;
using DataCenter.Model.Interface;
using DataCenter.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataCenter.ServiceCommand
{
    public class UpdateWarrantyMessageServiceCommand : ProviderQueueCommand<IProviderFactory, WarrantyMessageRecord, ResponseRecord>
    {
        public UpdateWarrantyMessageServiceCommand(IProviderFactory provider) : base(provider) { }
        protected override void Validate(WarrantyMessageRecord record)
        {
            if (record == null) throw new ArgumentNullException("request");
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            var rdcHist = Record;
            var obj = new WarrantyMessage()
            {
                ConfirmUser = rdcHist.ConfirmUser,
                EndDate = rdcHist.EndDate,
                EndUserCell = rdcHist.EndUserCell,
                LastUpdateDate = DateTime.Now,
                Model = rdcHist.Model,
                RespMsg = rdcHist.ResponseMessage,
                RespType = rdcHist.ResponseType,
                SellinDate = rdcHist.SellinDate,
                SelloutDate = rdcHist.SelloutDate,
                SelloutTime = rdcHist.SelloutTime,
                SerialNo = rdcHist.SerialNumber,
                SmsYn = rdcHist.SmsYn.HasValue ? rdcHist.SmsYn.Value.ToBooleanString() : null,
                StartDate = rdcHist.StartDate,
                Suffix = rdcHist.Suffix,
                TransferDate = DateTime.Now,
                TransferFlag = BooleanString.TRUE,
            };

            var itemCheck = Providers.WarrantyMessage.Retrieve(rdcHist.SerialNumber, rdcHist.ResponseType);
            if (itemCheck == null)
            {
                var resultitem1 = Providers.WarrantyMessage.Add(obj);
            }
            else
            {
                var resultitem2 = Providers.WarrantyMessage.Update(obj);
            }
            return true;
        }
    }
}
